package com.hb.shopping.controller;

import com.hb.shopping.mapper.UserMapper;
import com.hb.shopping.pojo.ResultMessage;
import com.hb.shopping.pojo.User;
import com.hb.shopping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hb
 * @create 2020-04-10 09:47
 */
//@Controller
@RestController
public class UserManagerController {

    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public User queryById(@PathVariable("id") Integer id) {

        User user = userService.queryById(id);

        return user;
    }

    @PutMapping("/user")
    public ResultMessage updateUser(@RequestBody User user){
        System.out.println(user);
        return new ResultMessage(true, "更新成功!");
    }

    @GetMapping("/user/like/{name}")
    public List<User> queryByLikeName(@PathVariable String name) {
        return userService.queryByLikeName(name);
    }

    @DeleteMapping("/user/{id}")
    public ResultMessage deleteById(@PathVariable("id") Integer id) {
        try {
            userService.deleteById(id);
            return new ResultMessage(true, "删除成功!");
        } catch (Exception e) {
            return new ResultMessage(false, e.getMessage());
        }
    }

    @PostMapping("/user")
    public ResultMessage registUser(@RequestBody User user) {
        try {
            userService.insertUser(user);
            return new ResultMessage(true, "注册成功!");
        } catch (Exception e) {
            return new ResultMessage(false, e.getMessage());
        }
    }

}
